/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxAreaOfIsland = function(grid) {
  let maxDepth = 0, count = 1;
  function dfs(indexI, indexJ) {
    // 上
    if(indexI - 1 >= 0 && grid[indexI - 1][indexJ]) {
      grid[indexI - 1][indexJ] = 0;
      count++;
      dfs(indexI - 1, indexJ);
    }
    // 下
    if(indexI + 1 < grid.length && grid[indexI + 1][indexJ]) {
      grid[indexI + 1][indexJ] = 0;
      count++;
      dfs(indexI + 1, indexJ);
    }
    // 左
    if(indexJ - 1 >= 0 && grid[indexI][indexJ - 1]) {
      grid[indexI][indexJ - 1] = 0;
      count++;
      dfs(indexI, indexJ - 1);
    }
    // 右
    if(indexJ + 1 < grid[0].length && grid[indexI][indexJ + 1]) {
      grid[indexI][indexJ + 1] = 0;
      count++;
      dfs(indexI, indexJ + 1, count);
    }
    maxDepth = Math.max(maxDepth, count);
  }

  for(let i = 0; i < grid.length; i++) {
    for(let j = 0; j < grid[0].length; j++) {
      if(grid[i][j]) {
        count = 1;
        grid[i][j] = 0;
        dfs(i, j);
      }
    }
  }
  return maxDepth;
};
let grid = [
  [0,0,1,0,0,0,0,1,0,0,0,0,0],
  [0,0,0,0,0,0,0,1,1,1,0,0,0],
  [0,1,1,0,1,0,0,0,0,0,0,0,0],
  [0,1,0,0,1,1,0,0,1,0,1,0,0],
  [0,1,0,0,1,1,0,0,1,1,1,0,0],
  [0,0,0,0,0,0,0,0,0,0,1,0,0],
  [0,0,0,0,0,0,0,1,1,1,0,0,0],
  [0,0,0,0,0,0,0,1,1,0,0,0,0]
];
console.log(maxAreaOfIsland(grid));
grid = [[0,0,0,0,0,0,0,0]];
console.log(maxAreaOfIsland(grid));
grid = [
  [1,1,0,0,0],
  [1,1,0,0,0],
  [0,0,0,1,1],
  [0,0,0,1,1]
];
console.log(maxAreaOfIsland(grid));
grid = [[0,1,1],[1,1,0]];
console.log(maxAreaOfIsland(grid));
